package com.lin.cms.service.impl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UrlPathHelper;

import com.lin.cms.common.web.RequestUtils;
import com.lin.cms.common.web.springmvc.MessageResolver;
import com.lin.cms.dao.LogMapper;
import com.lin.cms.domain.CmsUser;
import com.lin.cms.domain.Log;
import com.lin.cms.domain.Site;
import com.lin.cms.service.LogService;
import com.lin.cms.web.CmsUtils;

@Service
public class LogServiceImpl implements LogService{
	
	public Log loginSuccess(HttpServletRequest request, CmsUser user,String title) {
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Site site = CmsUtils.getSite(request);
		Date date = new Date();
		Log log = save(Log.LOGIN_SUCCESS, site, user, uri, ip, date,MessageResolver.getMessage(request, title), null);
		return log;
	}
	
	public Log save(Integer category, Site site, CmsUser user,String url, String ip, Date date, String title, String content) {
		Log log = new Log();
		
		log.setSiteId(site.getSiteId());
		log.setUserId(user.getUserId());
		log.setCategory(category);
		log.setIp(ip);
		log.setLogTime(date);
		log.setUrl(url);
		log.setTitle(title);
		log.setContent(content);
		save(log);
		return log;
	}
	
	public Log save(Log bean) {
		logMapper.insertSelective(bean);
		return bean;
	}
	public Log loginFailure(HttpServletRequest request, String title,String content) {
		// TODO Auto-generated method stub
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		Log log = save(Log.LOGIN_FAILURE, null, null, uri, ip, date,
				MessageResolver.getMessage(request, title), content);
		return log;
	}
	
	@Autowired
	private LogMapper logMapper;
	
}
